#include "Enemies.h"

//constructor
Enemies::Enemies()
{
	BuildVertexBuffer();
	BuildIndexBuffer();
	mPosition = D3DXVECTOR3((rand() % (27 - -27)) + -27, (rand() % (19 - -19)) + -19, (rand() % (-50 - -150)) + -150);
	
	HR(D3DXCreateEffectFromFile(gD3DDevice, "Transform.fx",
		0, 0, D3DXSHADER_DEBUG, 0, &mFx, &mErrors));

	if (mErrors) {
		MessageBox(0, (char*)mErrors->GetBufferPointer(), 0, 0);
	}

	mhTech = mFx->GetTechniqueByName("TransformTech");

	mhWVP = mFx->GetParameterByName(0, "gWVP");

	// CheckDeviceCaps
	mFx->SetTechnique(mhTech);
}
//destructor
Enemies::~Enemies()
{

}
//updating enemies positions
void Enemies::Update(double dt)
{	
	if (mPosition.z >= 50)
	{
		mPosition.z = -200;
	}
	else
	{
		mPosition.z += mVelocity * dt;
	}
}
//
void Enemies::Draw(ID3DXSprite& sprite, D3DXMATRIX& view, D3DXMATRIX& proj)
{
	HR(gD3DDevice->SetStreamSource(0, mVB, 0, sizeof(VertexPosCol)));
	HR(gD3DDevice->SetVertexDeclaration(VertexPosCol::decl));
	HR(gD3DDevice->SetIndices(mIB));
	//drawing enemies
	D3DXMATRIX move;
	D3DXMatrixTranslation(&move, mPosition.x, mPosition.y, mPosition.z);
	mFx->SetMatrix(mhWVP, &(move * view * proj));

	UINT numPasses = 0;
	mFx->Begin(&numPasses, 0);
	for (UINT i = 0; i < numPasses; i++)
	{
		mFx->BeginPass(i);
		HR(gD3DDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST, 0, 0, NB_VERTICES, 0, NB_FACES));
		mFx->EndPass();
	}
	mFx->End();
}
//VertexBuffer
void Enemies::BuildVertexBuffer()
{
	HR(gD3DDevice->CreateVertexBuffer(NB_VERTICES * sizeof(VertexPosCol)
		, D3DUSAGE_WRITEONLY, 0, D3DPOOL_MANAGED, &mVB, 0));

	VertexPosCol* v = 0;
	mVB->Lock(0, 0, (void**)&v, 0);

	v[0] = VertexPosCol(-1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));
	v[1] = VertexPosCol(-1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));
	v[2] = VertexPosCol(1.0f, 1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));
	v[3] = VertexPosCol(1.0f, -1.0f, -1.0f, D3DCOLOR_XRGB(255, 0, 0));
	v[4] = VertexPosCol(-1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0));
	v[5] = VertexPosCol(-1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0));
	v[6] = VertexPosCol(1.0f, 1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0));
	v[7] = VertexPosCol(1.0f, -1.0f, 1.0f, D3DCOLOR_XRGB(255, 0, 0));

	mVB->Unlock();
}
//IndexBuffer
void Enemies::BuildIndexBuffer()
{
	HR(gD3DDevice->CreateIndexBuffer(36 * sizeof(WORD)
		, D3DUSAGE_WRITEONLY, D3DFMT_INDEX16
		, D3DPOOL_MANAGED, &mIB, 0));

	WORD* k = 0;
	HR(mIB->Lock(0, 0, (void**)&k, 0));

	// Front face
	k[0] = 0; k[1] = 1; k[2] = 2;
	k[3] = 0; k[4] = 2; k[5] = 3;

	// Back face.
	k[6] = 4; k[7] = 6; k[8] = 5;
	k[9] = 4; k[10] = 7; k[11] = 6;

	// Left face.
	k[12] = 4; k[13] = 5; k[14] = 1;
	k[15] = 4; k[16] = 1; k[17] = 0;

	// Right face.
	k[18] = 3; k[19] = 2; k[20] = 6;
	k[21] = 3; k[22] = 6; k[23] = 7;

	// Top face.
	k[24] = 1; k[25] = 5; k[26] = 6;
	k[27] = 1; k[28] = 6; k[29] = 2;

	// Bottom face.
	k[30] = 4; k[31] = 0; k[32] = 3;
	k[33] = 4; k[34] = 3; k[35] = 7;

	HR(mIB->Unlock());
}
//getting enemie position for collisions
D3DXVECTOR3 Enemies::GetPosition()
{
	return mPosition;
}